import { getValidateTablename } from "@/api/databasemanage/databaselink";
export const validateDataTableConfiguration = async (
  dbLinkId,
  tableInfo,
  editOldDbId = "",
  editOldDbTableNames = []
) => {
  let returnValidate = { validate: false, message: "" };
  if (tableInfo && tableInfo.length > 0) {
    let message = "";
    let tableMessage = "";
    let tableNameList = [];
    tableInfo.forEach((ele1,index1) => {
      let formName = ele1.formName;
      if(formName==""){
        message += `第${index1}个的表名为空 `;
      }else{
		let pattern = new RegExp(/^[a-zA-Z]+[a-zA-Z0-9_]*$/gi);
		if (!pattern.test(formName)) {
			message +='表名必需以字母开头,只能包括字母、数字、下划线';				
		}
        if (ele1.dataChildren.length > 0) {
          let fieldNameArr = [];
          let fieldEmptyNameArr = ele1.dataChildren.filter(ele3=>{
            return ele3.fieldName=="";
          });
          if(fieldEmptyNameArr.length>0){
            message += `表名为${formName}下，字段名有为空`;
          }else{			  
            fieldNameArr = ele1.dataChildren.map(ele2 => {
			  let pattern = new RegExp(/^[a-zA-Z]+[a-zA-Z0-9_]*$/gi);
			  if (!pattern.test(ele2.fieldName)) {
			  	message +='字段名必需以字母开头,只能包括字母、数字、下划线';				
			  }
              return ele2.fieldName;
            });
            let fieldNameSetArr = [...new Set(fieldNameArr)];
            if (fieldNameArr.length != fieldNameSetArr.length) {
              message += `表名为${formName}下，字段名有重复`;
            }
          }
          
        }
        tableNameList.push(ele1.formName);
      }
      
    });
    let tableNameListSetArr = [...new Set(tableNameList)];
    if (tableNameList.length != tableNameListSetArr.length) {
      tableMessage += `表名重复`;
    }
    if (tableMessage) {
      returnValidate.validate = false;
      returnValidate.message = tableMessage;
      return returnValidate;
    } else {
      if (message) {
        returnValidate.validate = false;
        returnValidate.message = message;
        return returnValidate;
      } else {
        returnValidate.validate = true;
      }
    }
    if (returnValidate.validate) {
      /*
       是否需要后端接口验证 
        1 编辑时，数据库选择变更，需要验证
        2 编辑时，数据库未变更，表名跟原始设置的没变，不需要验证,反之需要验证
        3 新增时，需要验证
      */
      let needCheckTableNameList = [];
      let needCheck = false;
      if (editOldDbId) {
        if (dbLinkId == editOldDbId) {
          for (let index = 0; index < tableNameList.length; index++) {
            if (tableNameList[index] != editOldDbTableNames[index]) {
              needCheckTableNameList.push(tableNameList[index]);
            }
          }
          if (needCheckTableNameList.length > 0) {
            needCheck = true;
          }
        } else {
          needCheckTableNameList = tableNameList;
          needCheck = true;
        }
      } else {
        needCheckTableNameList = tableNameList;
        needCheck = true;
      }
      if (needCheck) {
        let tableNameStr = needCheckTableNameList.join(",");
        try {
          let res = await getValidateTablename(dbLinkId, tableNameStr);
          if (res.data.success) {
            returnValidate.validate = true;
          } else {
            returnValidate.message = res.data.msg;
            returnValidate.validate = false;
          }
        } catch (error) {
          returnValidate.message = '表名重复';
          returnValidate.validate = false;
        }
      } else {
        returnValidate.validate = true;
      }
      return returnValidate;
    }
  } else {
    returnValidate.message = "必须配置表才可以提交";
    return returnValidate;
  }
};
